Passed
Push — master ( 06bde6...6d2f62 )
by Paul
04:59
created

pinned.js ➔ Pinned   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
/** global: GLSR, jQuery */
2
;(function( x ) {
3
4
	'use strict';
5
6
	var Pinned = function() {
7
		this.el = x( '#pinned-status-select' );
8
		if( this.el ) {
9
			this.cancel = x( 'a.cancel-pinned-status' );
10
			this.cancel.on( 'click', this.onClickCancel_.bind( this ));
11
			this.edit = x( 'a.edit-pinned-status' );
12
			this.edit.on( 'click', this.onClickEdit_.bind( this ));
13
			this.save = x( 'a.save-pinned-status' );
14
			this.save.on( 'click', this.onClickSave_.bind( this ));
15
		}
16
		x( 'table td.pinned i' ).on( 'click', this.onClickToggle_.bind( this ));
17
	};
18
19
	Pinned.prototype = {
20
		/** @return void */
21
		restoreEditLink_: function() {
22
			this.el.slideUp( 'fast' );
23
			this.edit.show().focus();
24
		},
25
26
		/** @return void */
27
		onClickCancel_: function( ev ) { // MouseEvent
28
			ev.preventDefault();
29
			this.restoreEditLink_();
30
			this.el.find( 'select' ).val( x( '#hidden-pinned-status' ).val() === '0' ? 1 : 0 );
31
		},
32
33
		/** @return void */
34
		onClickEdit_: function( ev ) { // MouseEvent
35
			ev.preventDefault();
36
			if( !this.el.is( ':hidden' ))return;
37
			this.el.slideDown( 'fast', function() {
38
				this.el.find( 'select' ).focus();
39
			}.bind( this ));
40
			this.edit.hide();
41
		},
42
43
		/** @return void */
44
		onClickSave_: function( ev ) { // MouseEvent
45
			ev.preventDefault();
46
			this.restoreEditLink_();
47
			this.target = ev.target;
48
			var request = {
49
				action: 'toggle-pinned',
50
				id: x( '#post_ID' ).val(),
51
				pinned: x( '#pinned-status' ).val(),
52
			};
53
			(new GLSR.Ajax( request )).post( this.save_.bind( this ));
54
		},
55
56
		/** @return void */
57
		onClickToggle_: function( ev ) { // MouseEvent
58
			ev.preventDefault();
59
			this.target = ev.target;
60
			var request = {
61
				action: 'toggle-pinned',
62
				id: ev.target.getAttribute( 'data-id' ),
63
			};
64
			(new GLSR.Ajax( request )).post( this.togglePinned_.bind( this ));
65
		},
66
67
		/** @return void */
68
		save_: function( response ) {
69
			x( '#pinned-status' ).val( !response.pinned|0 );
70
			x( '#hidden-pinned-status' ).val( response.pinned|0 );
71
			x( '#pinned-status-text' ).text( response.pinned ? this.target.dataset.yes : this.target.dataset.no );
72
			GLSR.Notices( response.notices );
73
		},
74
75
		/** @return void */
76
		togglePinned_: function( response ) {
77
			this.target.classList[response.pinned ? 'add' : 'remove']( 'pinned' );
78
		},
79
	};
80
81
	GLSR.Pinned = Pinned;
82
})( jQuery );
83